package hot100.h0912;

import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;

public class JudgeBracket {
    static HashMap<Character, Character> map = new HashMap<>();

    static {
        map.put('(', ')');
        map.put('[', ']');
        map.put('{', '}');
    }

    public static boolean isValid(String s) {
        Deque<Character> stack = new LinkedList<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (map.containsKey(c)) {
                stack.push(c);
            } else {
                if (stack.isEmpty()){
                    return false;
                }
                if (map.get(stack.peek()) == c) {
                    stack.pop();
                }
            }
        }
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        String s="([]())";
        System.out.println(isValid(s));
    }
}
